unit CondicaoPagTransRecordFr;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, DBCtrls, ActnList, ComCtrls, DBGrids, MainTransactionRecordFr;

type

  { TCondicaoPagTransRecordFrame }

  TCondicaoPagTransRecordFrame = class(TMainTransactionRecordFrame)
    CancelarCondAction: TAction;
    SalvarCondAction: TAction;
    AlterarCondAction: TAction;
    CondicaoActionList: TActionList;
    desc_condicao_pagamento: TDBEdit;
    DetailDBGrid: TDBGrid;
    DetailDBNavigator: TDBNavigator;
    DetailGroupBox: TGroupBox;
    id_condicao_pagamento: TDBEdit;
    Label1: TLabel;
    Label2: TLabel;
    MasterGroupBox: TGroupBox;
    ToolBar1: TToolBar;
    AlterarParcelaButton: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton9: TToolButton;
    procedure AlterarCondActionExecute(Sender: TObject);
    procedure CancelarCondActionExecute(Sender: TObject);
    procedure ExcluirActionExecute(Sender: TObject);
    procedure InserirActionExecute(Sender: TObject);
    procedure SalvarCondActionExecute(Sender: TObject);
    procedure ControlaBotoes(); override;
  end;

implementation

uses
  MainFr, CondicaoPagamentoDM, TransactionDM, MainDM;

{ TCondicaoPagTransRecordFrame }

procedure TCondicaoPagTransRecordFrame.AlterarCondActionExecute(Sender: TObject);
begin
  CondicaoPagamentoData.CondPagParcelaZQuery.Edit();
  ControlaBotoes();
end;

procedure TCondicaoPagTransRecordFrame.CancelarCondActionExecute(Sender: TObject);
begin
  CondicaoPagamentoData.RemoveParcelas(id_condicao_pagamento.Text);
  CondicaoPagamentoData.CondPagParcelaDatasource.DataSet.Refresh();
  ControlaBotoes();
end;

procedure TCondicaoPagTransRecordFrame.ExcluirActionExecute(Sender: TObject);
begin
  AlterarCondAction.Enabled := False;
  CondicaoPagamentoData.RemoveParcelas(id_condicao_pagamento.Text);
  CondicaoPagamentoData.CondicaoPagamentoZQuery.Delete();
  ControlaBotoes();
end;

procedure TCondicaoPagTransRecordFrame.InserirActionExecute(Sender: TObject);
begin
  CondicaoPagamentoData.LimpaParcelas();
  CondicaoPagamentoData.CondicaoPagamentoZQuery.Insert();
  AlterarCondAction.Enabled:=false;
  ControlaBotoes();
end;

procedure TCondicaoPagTransRecordFrame.SalvarCondActionExecute(Sender: TObject);
var
  Verifica: boolean;
begin
  Verifica := CondicaoPagamentoData.VerificaTotalPorcentagem(
    id_condicao_pagamento.Text);
  if (Verifica = True) then begin
    ControlaBotoes();
  end else begin
    ShowMessage('O Somatorio da Porcentagem tem que dar 100!');
  end;
end;

procedure TCondicaoPagTransRecordFrame.ControlaBotoes();
var
  I: integer;
begin
 if (AlterarCondAction.Enabled = True) then
  begin
    SalvarCondAction.Enabled := True;
    CancelarCondAction.Enabled := True;
    AlterarCondAction.Enabled := False;
    // Desabilita todas as ações
    for I := 0 to RecordActionList.ActionCount - 1 do
    begin
      TAction(RecordActionList.Actions[I]).Enabled := False;
    end;
    RecordDBNavigator.Enabled := False;
    DetailDBNavigator.Enabled := True;
    DetailDBGrid.Enabled := True;
  end
  else
  begin
    inherited ControlaBotoes();
    DetailDBNavigator.Enabled := False;
    DetailDBGrid.Enabled := False;
    RecordDBNavigator.Enabled := True;
    SalvarCondAction.Enabled := False;
    CancelarCondAction.Enabled := False;
    AlterarCondAction.Enabled := AlterarAction.Enabled;
  end;
end;



initialization
  {$I condicaopagtransrecordfr.lrs}

end.
